﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Strom.Core.Accounts;
using Strom.Core.Config;
using Strom.Core.Graphic;
using Strom.Core.Network;
using Strom.Core.Network.Packets;
using Strom.MapEditor.Network;

namespace Strom.MapEditor.GUI
{
    public partial class frmLogin : Form, ILoginForm
    {
        delegate void ShowFormEditor(BaseClient client, List<Core.Characters.Character> chars);

        public frmLogin()
        {
            InitializeComponent();
            MapEditorManager.GUI = new GUIManager {Login = this};
            Loader.Load();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;
        }

        public void RegisterSuccess(Core.Network.BaseClient client)
        {
            throw new NotImplementedException();
        }

        void TryToEnableConnectButton()
        {
            if (textBox1.Text.Length > 3 & textBox2.Text.Length > 3)
                button1.Enabled = true;
            else
                button1.Enabled = false;
        }

        public void RegisterFailed(BaseClient client)
        {
            throw new NotImplementedException();
        }

        protected override void OnClosed(EventArgs e)
        {
            AutoConfig.SaveConfig("Config.xml");
            if (MapEditorManager.GUI.Editor != null)
                MapEditorManager.GUI.Editor.Dispose();
            base.OnClosed(e);
            base.OnClosed(e);
        }

        public void ConnectSuccess(BaseClient client, List<Core.Characters.Character> characters)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new ShowFormEditor(ConnectSuccess), new object[] { client, null });
            }
            else
            {
                MapEditorManager.Client = client;
                Hide();
                new frmEditor(textBox1.Text).Show();
            }
        }

        public void ConnectFailed(BaseClient client)
        {
            client.Disconnect();
            MessageBox.Show("Connexion à ce compte impossible. Il est possible que vos identifiants soient incorrects ou que quelqu'un soit déjà connecté sur ce compte.", "Strøm", MessageBoxButtons.OK, MessageBoxIcon.Error);
            button1.Enabled = true;
            textBox1.Enabled = true;
            textBox2.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            textBox1.Enabled = false;
            textBox2.Enabled = false;
            var client = new BaseClient();
            Parser.RegisterNewClientOnParser(client);

            try
            {
                client.Connect(RemoteServerConfig.ServerIP, RemoteServerConfig.ServerPort);
            }
            catch
            {
                MessageBox.Show("Impossible de se connecter au serveur.", "Strøm", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                button1.Enabled = true;
                textBox1.Enabled = true;
                textBox2.Enabled = true;
            }

            if (client.IsConnected)
                new LoginRequestPacket().Pack(client, (byte)2, textBox1.Text, textBox2.Text);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            TryToEnableConnectButton();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            TryToEnableConnectButton();
        }
    }
}
